﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Microsoft.VisualStudio.CommandBars;
using Rhino.Mocks;
using System.Reflection;
using EnvDTE80;
using EnvDTE;
using TSVNAddin.Core;

namespace TSVNAddin.VS.Test
{
	[TestFixture]
	public class UIConfiguratorFixture
	{
		[Test]
		public void SetupUI()
		{
			var commandsBuilder = MockRepository.GenerateMock<VSCommandsBuilder>(null, null);

			var cmdDiff = MockRepository.GenerateStub<Command>();
			var cmdUpdate = MockRepository.GenerateStub<Command>();
			var cmdCommit = MockRepository.GenerateStub<Command>();
			var cmdShowLog = MockRepository.GenerateStub<Command>();
			var cmdRepoBrowser = MockRepository.GenerateStub<Command>();
			var cmdShowChanges = MockRepository.GenerateStub<Command>();
			var cmdRevisionGraph = MockRepository.GenerateStub<Command>();
			var cmdUpdateToRevision = MockRepository.GenerateStub<Command>();
			var cmdRevert = MockRepository.GenerateStub<Command>();
			var cmdGetLock = MockRepository.GenerateStub<Command>();
			var cmdReleaseLock = MockRepository.GenerateStub<Command>();
			var cmdBranchTag = MockRepository.GenerateStub<Command>();
			var cmdSwitch = MockRepository.GenerateStub<Command>();
			var cmdMerge = MockRepository.GenerateStub<Command>();
			var cmdCreatePatch = MockRepository.GenerateStub<Command>();
			var cmdApplyPatch = MockRepository.GenerateStub<Command>();
			var cmdProperties = MockRepository.GenerateStub<Command>();
			var cmdSettings = MockRepository.GenerateStub<Command>();
			var cmdCheckout = MockRepository.GenerateStub<Command>();
			var cmdCreateRepository = MockRepository.GenerateStub<Command>();
			var cmdAboutTortoiseSVN = MockRepository.GenerateStub<Command>();
			var cmdAboutTortoiseSVNAddin = MockRepository.GenerateStub<Command>();
			var cmdSetWorkingRoot = MockRepository.GenerateStub<Command>();

			commandsBuilder.Expect(x => x.AddCommand(CommandType.ActiveDiff)).Return(cmdDiff);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootUpdate)).Return(cmdUpdate);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootCommit)).Return(cmdCommit);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootShowLog)).Return(cmdShowLog);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootRepoBrowser)).Return(cmdRepoBrowser);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootShowChanges)).Return(cmdShowChanges);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootRevisionGraph)).Return(cmdRevisionGraph);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootUpdateToRevision)).Return(cmdUpdateToRevision);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootRevert)).Return(cmdRevert);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootGetLock)).Return(cmdGetLock);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootReleaseLock)).Return(cmdReleaseLock);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootBranchTag)).Return(cmdBranchTag);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootSwitch)).Return(cmdSwitch);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootMerge)).Return(cmdMerge);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootCreatePatch)).Return(cmdCreatePatch);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootApplyPatch)).Return(cmdApplyPatch);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.RootProperties)).Return(cmdProperties);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.Settings)).Return(cmdSettings);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.Checkout)).Return(cmdCheckout);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.CreateReposiotry)).Return(cmdCreateRepository);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.AboutTortoiseSVN)).Return(cmdAboutTortoiseSVN);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.AboutTortoiseSVNAddin)).Return(cmdAboutTortoiseSVNAddin);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.SetWorkingRoot)).Return(cmdSetWorkingRoot);

			var tortoriseMenuBar = MockRepository.GenerateMock<CommandBarAdapter>(null, null);
			tortoriseMenuBar.Expect(x => x.AddCommands(
				cmdDiff,
				cmdUpdate,
				cmdCommit,
				cmdShowLog,
				cmdRepoBrowser,
				cmdShowChanges,
				cmdUpdateToRevision,
				cmdRevisionGraph,
				cmdRevert,
				cmdGetLock,
				cmdReleaseLock,
				cmdBranchTag,
				cmdSwitch,
				cmdMerge,
				cmdCreatePatch,
				cmdApplyPatch,
				cmdProperties,
				cmdSetWorkingRoot,
				cmdSettings,
				cmdCheckout,
				cmdCreateRepository,
				cmdAboutTortoiseSVN,
				cmdAboutTortoiseSVNAddin));

			var menuBar = MockRepository.GenerateStub<IMainBarAdapter>();
			menuBar.Stub(x => x.AddTortoiseCommandBar()).Return(tortoriseMenuBar);

			var tortoriseToolBar = MockRepository.GenerateMock<CommandBarAdapter>(null, null);
			tortoriseToolBar.Expect(x => x.AddCommands(
				cmdDiff,
				cmdUpdate,
				cmdCommit,
				cmdShowChanges,
				cmdSetWorkingRoot));

			var toolBar = MockRepository.GenerateStub<IMainBarAdapter>();
			toolBar.Stub(x => x.AddTortoiseCommandBar()).Return(tortoriseToolBar);

			var cmdItemDiff = MockRepository.GenerateStub<Command>();
			var cmdItemUpdate = MockRepository.GenerateStub<Command>();
			var cmdItemCommit = MockRepository.GenerateStub<Command>();
			var cmdItemShowLog = MockRepository.GenerateStub<Command>();
			var cmdItemRepoBrowser = MockRepository.GenerateStub<Command>();
			var cmdItemShowChanges = MockRepository.GenerateStub<Command>();
			var cmdItemUpdateToRevision = MockRepository.GenerateStub<Command>();
			var cmdItemRevert = MockRepository.GenerateStub<Command>();
			var cmdItemGetLock = MockRepository.GenerateStub<Command>();
			var cmdItemReleaseLock = MockRepository.GenerateStub<Command>();
			var cmdItemBranchTag = MockRepository.GenerateStub<Command>();
			var cmdItemSwitch = MockRepository.GenerateStub<Command>();
			var cmdItemMerge = MockRepository.GenerateStub<Command>();
			var cmdItemBlame = MockRepository.GenerateStub<Command>();
			var cmdItemCreatePatch = MockRepository.GenerateStub<Command>();
			var cmdItemProperties = MockRepository.GenerateStub<Command>();

			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemDiff)).Return(cmdItemDiff);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemUpdate)).Return(cmdItemUpdate);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemCommit)).Return(cmdItemCommit);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemShowLog)).Return(cmdItemShowLog);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemRepoBrowser)).Return(cmdItemRepoBrowser);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemShowChanges)).Return(cmdItemShowChanges);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemUpdateToRevision)).Return(cmdItemUpdateToRevision);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemRevert)).Return(cmdItemRevert);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemGetLock)).Return(cmdItemGetLock);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemReleaseLock)).Return(cmdItemReleaseLock);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemBranchTag)).Return(cmdItemBranchTag);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemSwitch)).Return(cmdItemSwitch);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemMerge)).Return(cmdItemMerge);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemBlame)).Return(cmdItemBlame);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemCreatePatch)).Return(cmdItemCreatePatch);
			commandsBuilder.Expect(x => x.AddCommand(CommandType.ItemProperties)).Return(cmdItemProperties);

			var tortoriseSolutionBar = MockRepository.GenerateMock<CommandBarAdapter>(null, null);
			tortoriseSolutionBar.Expect(x => x.AddCommands(
					cmdItemDiff,
					cmdItemUpdate,
					cmdItemCommit,
					cmdItemShowLog,
					cmdItemRepoBrowser,
					cmdItemShowChanges,
					cmdItemUpdateToRevision,
					cmdItemRevert,
					cmdItemGetLock,
					cmdItemReleaseLock,
					cmdItemBranchTag,
					cmdItemSwitch,
					cmdItemMerge,
					cmdItemBlame,
					cmdItemCreatePatch,
					cmdItemProperties
				));

			var solutionBar = MockRepository.GenerateStub<IMainBarAdapter>();
			solutionBar.Stub(x => x.AddTortoiseCommandBar()).Return(tortoriseSolutionBar);

			var dteAdapter = MockRepository.GenerateStub<DTEAdapter>((DTE2)null);
			dteAdapter.Stub(x => x.GetMenuBar()).Return(menuBar);
			dteAdapter.Stub(x => x.GetToolBar()).Return(toolBar);
			dteAdapter.Stub(x => x.GetSolutionBar()).Return(solutionBar);

			var uiConfigurator = new UIConfigurator(commandsBuilder, dteAdapter);
			uiConfigurator.SetupUI();

			commandsBuilder.VerifyAllExpectations();
			tortoriseMenuBar.VerifyAllExpectations();
			tortoriseToolBar.VerifyAllExpectations();
			tortoriseSolutionBar.VerifyAllExpectations();
		}
	}
}
